package com.bot.nft.module.chain.controller;

import com.alibaba.fastjson.JSON;
import com.bot.nft.core.enums.chain.ChainLogEnum;
import com.bot.nft.core.enums.dc.NftCertEnum;
import com.bot.nft.module.chain.domain.CallBackInfo;
import com.bot.nft.module.chain.domain.ChainOptLogInfo;
import com.bot.nft.module.chain.service.ChainOptLogServer;
import com.bot.nft.module.dc.domain.bo.DcNftCertBo;
import com.bot.nft.module.dc.service.IDcNftCertService;
import com.bot.nft.module.dc.service.IDcNftInfoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import static com.bot.nft.core.config.AvataUtils.signRequest;

@RestController
@RequestMapping("chain")
public class CallbackController {

    @Resource
    private ChainOptLogServer chainOptLogServer;

    @Resource
    private IDcNftInfoService dcNftInfoService;

    //藏品证书接口
    @Resource
    private IDcNftCertService iDcNftCertService;

    @RequestMapping("chain")
    public String nftnotify(HttpServletRequest request, HttpServletResponse response, @RequestBody String data) {
        //验证签名
        String timestamp = request.getHeader("X-Timestamp");
        String qianming = request.getHeader("X-Signature");
        HashMap map = JSON.parseObject(data, HashMap.class);
        String signature = signRequest("chain/nftnotity", null, map, Integer.valueOf(timestamp), "api-secret");
        if (!signature.equals(qianming)) return "failed";
        //成功之后，执行业务逻辑 修改状态
        String optId = (String) map.get("operation_id");
        //用操作Id查询日志表
        CallBackInfo callBackInfo = JSON.parseObject(data, CallBackInfo.class);
        //类型为1，类型为2........
        Integer status = callBackInfo.getStatus();
        //为1 修改数字藏品发售状态，更新class_id
        if (status == ChainLogEnum.SUCCEEDSTATUS.getValue()) {
            //根据operation_id查看日志表
            ChainOptLogInfo col = chainOptLogServer.getCOL(callBackInfo.getOperationId());
            //修改日志表状态为成功
            //同步更新日志表状态为成功
            col.setStatus(ChainLogEnum.SUCCEEDSTATUS.getValue());
            //调用接口
            Boolean boo = chainOptLogServer.updateCPLog(col);
            //3.处理
            Integer chainOptType = col.getChainOptType();
            String objId = col.getObjId();
            String userId = col.getUserId();
            //判断boo，更新class_id
            if (boo) {
                //状态更新成功的话就更新具体表的classId
                CallBackInfo.Nft nft = callBackInfo.getNft();
                String classId = nft.getClassId();
                //取出来classId后修改具体的表格中的链上标识
                //判断具体业务 判断是不是创建藏品类别
                if (col.getChainOptType() == ChainLogEnum.CHAIN_PUBLISH_NFT.getValue()) {
                    Boolean dcBoo = dcNftInfoService.updataNftContract(objId, classId);
                }
                //发行NFT
                if (chainOptType.equals(ChainLogEnum.CHAIN_PUBLISH_NFT.getValue())) {
                    //保存藏品证书链上标识，藏品证书持有人
                    DcNftCertBo dcNftCert = new DcNftCertBo();
                    dcNftCert.setId(objId);
                    dcNftCert.setChainAddr(nft.getId());
                    dcNftCert.setMbId(userId);
                    Boolean flag = iDcNftCertService.updateCert(dcNftCert);
                }
                //转让NFT
                if (chainOptType.equals(ChainLogEnum.CHAIN_MAKE_OVER_NFT.getValue())) {

                    //保存藏品证书链上标识，藏品证书持有人
                    DcNftCertBo dcNftCert = new DcNftCertBo();
                    dcNftCert.setId(objId);
                    dcNftCert.setMbId(userId);
                    Boolean flag = iDcNftCertService.updateCert(dcNftCert);

                }
                //销毁NFT
                if (chainOptType.equals(ChainLogEnum.CHAIN_MAKE_OVER_NFT.getValue())) {

                    //保存藏品证书链上标识，藏品证书持有人
                    DcNftCertBo dcNftCert = new DcNftCertBo();
                    dcNftCert.setId(objId);
                    dcNftCert.setChainAddr("");
                    dcNftCert.setStatus(NftCertEnum.DESTROYED.getValue());
                    Boolean flag = iDcNftCertService.updateCert(dcNftCert);

                }
            }
        }
        return "SUCCESS";
    }
}
